/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense.units;

import CastleDefense.Animation;
import CastleDefense.Game;
import java.awt.Image;
import javax.vecmath.Vector2d;

/**
 *
 * @author Firedemon
 */
public class UnitMage extends PlacedUnit
{
    private Animation Mage;

    public UnitMage(Vector2d loc, Image img, String name)
    {
        super(loc,img,name);

        Mage = new Animation(getAnimation(name), 10);
        this.setAnimation(Mage);
    }

    @Override
    public void doLogic()
    {
        //ice sorc
        if(this.getName().equals("Ice Sorcerer"))
        {
            if(getTarget() != null && !getTarget().isDead())
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                    return;

                lastFire = System.currentTimeMillis();
                
                Mage.start();
                if(Mage.isDone())
                {
                    Mage.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else
            {
                setClosestTarget();
            }
            
            return;
        }
        else if(this.getName().equals("Fire Mage"))
        {
            if(getTarget() != null && !getTarget().isDead()) //tries to fireEnemy at a target
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                    return;

                lastFire = System.currentTimeMillis();
                
                Mage.start();
                if(Mage.isDone())
                {
                    Mage.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
            {
                setClosestTarget();
            }
            else //resets a target if it is too far away
            {
                setClosestTarget();
            }
        }
        else if(this.getName().equals("Light Druid"))
        {
            if(getTarget() != null && !getTarget().isDead()) //tries to fireEnemy at a target
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                    return;

                lastFire = System.currentTimeMillis();
                
                Mage.start();
                if(Mage.isDone())
                {
                    Mage.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
            {
                setClosestTarget();
            }
            else //resets a target if it is too far away
            {
                setClosestTarget();
            }
        }
        else if(this.getName().equals("Earth Elemental"))
        {
            if(getTarget() != null && !getTarget().isDead()) //tries to fireEnemy at a target
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                    return;

                lastFire = System.currentTimeMillis();

                Mage.start();
                if(Mage.isDone())
                {
                    Mage.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
            {
                setClosestTarget();
            }
            else //resets a target if it is too far away
            {
                setClosestTarget();
            }
        }
    }
}
